package web.backend;

import java.io.IOException;import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Easybuy_order_detail;
import entity.Easybuy_product;
import entity.Easybuy_user;
import service.EasyBuyProductService;
import service.OrderDetailService;
import service.OrderService;
import service.Impl.EasyBuyProductServiceImpl;
import service.Impl.OrderDetailServiceImpl;
import service.Impl.OrderServiceImpl;
import utils.PageUtils;

/**
 * Servlet implementation class AdminOrder
 */
public class AdminOrder extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private OrderDetailService service=new OrderDetailServiceImpl();
	private OrderService order=new OrderServiceImpl();
	private EasyBuyProductService productService=new EasyBuyProductServiceImpl();
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String action=request.getParameter("action");
		switch (action) {
		case "index":
			index(request,response);
			break;
		case "queryAllOrder":
			queryAllOrder(request,response);
			break;
		default:
			break;
		}
	}

	private void queryAllOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int currpage=Integer.parseInt(request.getParameter("currpage"));
		int pagesize=6;
		
		PageUtils<Easybuy_order_detail> utils=service.orderdetailList(currpage, pagesize);
		
		for (Easybuy_order_detail order : utils.getList()) {
			order.setProduct(productService.productId(order.getProductId()));
		}
		request.setAttribute("utils", utils);
		utils.setPageindex(currpage);
		request.getRequestDispatcher("/backend/orderDetail/OrderDetail.jsp").forward(request, response);
		
	}

	private void index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		int userId=Integer.parseInt(request.getParameter("userId"));
		
		int orderId=order.orderId(userId);
		
		int currpage=Integer.parseInt(request.getParameter("currpage"));
		int pagesize=6;
		
		PageUtils<Easybuy_order_detail> utils=service.orderList(orderId, currpage, pagesize);
		
		for (Easybuy_order_detail order : utils.getList()) {
			order.setProduct(productService.productId(order.getProductId()));
		}
		request.setAttribute("utils", utils);
		utils.setPageindex(currpage);
		request.getRequestDispatcher("/backend/orderDetail/OrderDetail.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
